package com.molyfun.parents.fragment.main;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.setting.MineActivity;
import com.molyfun.parents.adapter.MineClassRoomAdapter;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.im.MyTIMMsgListener;
import com.molyfun.parents.im.model.BaseMessageEntity;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.TeamListResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.view.swipeRefreshLayout.SwipeRefreshLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by ZhaoWei on 2016/12/5.
 * 首页-我的班级
 */

public class MineClassRoomFragment extends BaseFragment implements View.OnClickListener, UIEventListener, SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener,
        MineClassRoomAdapter.MainClassRoomInterface, MyTIMMsgListener.ITIMMsgReceivedCallback {

    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @InjectView(R.id.no_data_layout)
    TextView mEmptyLayout;

    private LinearLayoutManager mLayoutManager;

    private MineClassRoomAdapter mMineClassRoomAdapter;

    protected int mCurrentPage = RestClient.DEFAULT_PAGE;
    private boolean mHasMoreData = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine_classroom, container, false);
        ButterKnife.inject(this, view);

        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setOnLoadListener(this);
        mSwipeRefreshLayout.setTopColor(R.color.refresh_color_01, R.color.refresh_color_02,
                R.color.refresh_color_02, R.color.refresh_color_01);
        mSwipeRefreshLayout.setBottomColor(R.color.color_transparent, R.color.color_transparent,
                R.color.color_transparent, R.color.color_transparent);
        mSwipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        mSwipeRefreshLayout.setLoadNoFull(true);

        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mMineClassRoomAdapter = new MineClassRoomAdapter(getContext(), this);
        mRecyclerView.setAdapter(mMineClassRoomAdapter);

        MyTIMMsgListener.addITIMMsgReceivedCallback(this);
        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_DATE_CHANGE, this);

        return view;
    }

    @Override
    public void onResume() {
        onRefresh();
        super.onResume();
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setLoading(false);
        mCurrentPage = RestClient.DEFAULT_PAGE;
        getSchoolLive();
    }

    @Override
    public void onMessage(BaseMessageEntity messageEntity) {
        switch (messageEntity.getEventtype()) {
            case MyTIMMsgListener.EventType.GROUP:
                if (messageEntity.getOpertype().equals(MyTIMMsgListener.OperType.LIVE_START) || messageEntity.getOpertype().equals(MyTIMMsgListener.OperType.LIVE_STOP)) {
                    //如果有小组直播开始或者结束，刷新列表
                    onRefresh();
                }
                break;
            case MyTIMMsgListener.EventType.IM_MSG:
                if(messageEntity.getOpertype().equals(MyTIMMsgListener.OperType.GROUP_MSG)){
                    mMineClassRoomAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    public void onUserInfoClick() {
        MineActivity.startActivity(getContext());
    }

    @Override
    public void onLoad() {
        if(mHasMoreData){
            mCurrentPage ++;
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
            getSchoolLive();
        }else{
            mSwipeRefreshLayout.setRefreshing(false);
            mSwipeRefreshLayout.setLoading(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        MyTIMMsgListener.removeITIMMsgReceivedCallback(this);
        MFPApplication.getInstance().getEventController().removeUIEventListener(EventDispatcherEnum.UI_EVENT_DATE_CHANGE, this);
    }


    private void getSchoolLive() {
        Call<TeamListResponse> call = MFPApplication.getInstance().getRestClient().getTeamList(mCurrentPage);
        call.enqueue(new Callback<TeamListResponse>() {
            @Override
            public void onResponse(Call<TeamListResponse> call, Response<TeamListResponse> response) {
                if (response.body() != null) {
                    if(response.body().getData() != null && response.body().getData().size() > 0){
                        mEmptyLayout.setVisibility(View.GONE);
                        mHasMoreData = true;
                        if(mCurrentPage == RestClient.DEFAULT_PAGE){
                            mMineClassRoomAdapter.refreshData(response.body().getData());
                        }else{
                            mMineClassRoomAdapter.addAll(response.body().getData());
                        }
                        mMineClassRoomAdapter.notifyDataSetChanged();
                    }else{
                        if(mCurrentPage == RestClient.DEFAULT_PAGE){
                            mEmptyLayout.setVisibility(View.VISIBLE);
                            mMineClassRoomAdapter.clearAll();
                        }
                        mHasMoreData = false;
                    }
                }

            }

            @Override
            public void onFailure(Call<TeamListResponse> call, Throwable t) {

            }
        });
    }


    @Override
    public void handleUIEvent(Message msg) {

        switch (msg.what) {

        }
    }

}
